#pragma once
#ifndef SIMPLEARRAY
#define SIMPLEARRAY

#include <iostream>
using namespace std;

template <class A>
class SimpleArray
{
public:
	SimpleArray(int arraySize, A init)
	{
		size = (arraySize > 0 ? arraySize : 10);
		ptr = new A[size];

		for (int i = 0; i < size; i++)
			ptr[i] = init;
	}

	SimpleArray(int arraySize)
	{
		size = (arraySize > 0 ? arraySize : 10);
		ptr = new A[size];
	}

	SimpleArray(const SimpleArray<A> &arrayToCopy) : size(arrayToCopy.size)
	{
		ptr = new A[size];

		for (int i = 0; i < size; i++)
			ptr[i] = arrayToCopy.ptr[i];
	}

	~SimpleArray()
	{
		delete[] ptr;
	}

	A* buffer() const
	{
		return ptr;
	}

	const SimpleArray<A> &copy(const SimpleArray<A> &right)
	{
		if (&right != this)
		{
			if (size != right.size)
			{
				delete[] ptr;
				size = right.size;
				ptr = new A[size];
			}

			for (int i = 0; i < size; i++)
				ptr[i] = right.ptr[i];
		}

		return *this;
	}

	bool equals(const SimpleArray<A> &right)
	{
		if (size != right.size)
			return false;

		for (int i = 0; i < size; i++)
			if (ptr[i] != right.ptr[i])
				return false;

		return true;
	}

	int length()
	{
		return size;
	}

	 A get(int index)
	{
		if (index < 0 || index >= size)
		{
			cerr << "\nError: index " << index << " out of range. Max size: " << size << endl;
			exit(1);
		}

		return ptr[index];
	}

	void set(int index, A value)
	{
		if (index < 0 || index >= size)
		{
			cerr << "\nError: index " << index << " out of range. Max size: " << size << endl;
			exit(1);
		}

		ptr[index] = value;
	}

	void print()
	{
		cout << "[ ";
		for (int i = 0; i < size; i++)
		{
			if (i == size - 1)
				cout << ptr[i];
			else
				cout << ptr[i] << " ";
		}
		cout << "]\n";
	}

private:
	int size;
	A *ptr;
};

#endif